<?php
class Server {
  public static function submit_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    if ($req->title == '') {
      echo 'Name is empty';
      return;
    }
    if ($req->rate == '') {
      echo 'Rate is empty';
      return;
    }
    
    if (is_numeric($req->rate) == false) {
      echo 'Rate must be a number';
      return;
    }
    
    if (isset($req->id)) {
      $orm->update('pb_ecom_currency', $req);
    } else {
      echo 'Disabled';
      return;
      $orm->add('pb_ecom_currency', $req);
      $req->id = $orm->last_id();
    }
    echo json_encode($req);
  }
  
  public static function set_default($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    $res = $orm->get('pb_ecom_currency', NULL, NULL, NULL, NULL, NULL)->d;
    Server::___update_rate($orm, $res, $req);
    echo json_encode($res);
  }
  
  public static function delete_obj($orm, $sys, $inst, $is_iframe) {
    echo 'Disabled';
    return;
    $req = json_decode(Utils::get_param('req', '[]'));
    $orm->delete('pb_ecom_currency', $req);
    $res = $orm->get('pb_ecom_currency', NULL, NULL, NULL, NULL, NULL)->d;
    if ($req->is_default == 1 && count($res) > 0) {
      Server::___update_rate($orm, $res, $res[0]);
    }
    echo json_encode($res);
  }
  
  public static function ___update_rate($orm, $res, $req) {
    $ratio = round(1 / $req->rate, 5);
    foreach ($res as &$itm) {
      if ($itm->id != $req->id) {
        $itm->rate = $itm->rate * $ratio;
        $itm->is_default = 0;
      } else {
        $itm->rate = 1;
        $itm->is_default = 1;
      }
      $orm->update('pb_ecom_currency', $itm);
    }
  }
}
?>